package org.ohos.rajawali3d.examples.examples.loaders;

import ohos.aafwk.content.Intent;
import ohos.agp.components.*;
import ohos.agp.utils.Color;
import ohos.agp.utils.LayoutAlignment;
import ohos.agp.utils.TextAlignment;
import ohos.app.Context;
import ohos.global.resource.RawFileEntry;
import ohos.global.resource.ResourceManager;
import org.ohos.rajawali3d.ResourceTable;
import org.ohos.rajawali3d.examples.examples.AExampleFragment;
import org.rajawali3d.Object3D;
import org.rajawali3d.animation.Animation;
import org.rajawali3d.animation.Animation3D;
import org.rajawali3d.animation.RotateOnAxisAnimation;
import org.rajawali3d.loader.ParsingException;
import org.rajawali3d.loader.fbx.LoaderFBX;
import org.rajawali3d.math.vector.Vector3;

public class FBXFragment extends AExampleFragment {

    @Override
    public Component onCreateView(LayoutScatter inflater, ComponentContainer container, Intent savedInstanceState) {
        super.onCreateView(inflater, container, savedInstanceState);
        DirectionalLayout ll = new DirectionalLayout(getContext());
        ComponentContainer.LayoutConfig lc = ll.getLayoutConfig();
        lc.width = DirectionalLayout.LayoutConfig.MATCH_PARENT;
        lc.height = DirectionalLayout.LayoutConfig.MATCH_PARENT;
        ll.setOrientation(DirectionalLayout.VERTICAL);
        ll.setAlignment(LayoutAlignment.BOTTOM);

        Text label = new Text(getContext());
        ComponentContainer.LayoutConfig layoutConfig = label.getLayoutConfig();
        layoutConfig.width = DirectionalLayout.LayoutConfig.MATCH_PARENT;
        layoutConfig.height = DirectionalLayout.LayoutConfig.MATCH_CONTENT;
        label.setText(ResourceTable.String_fbx_fragment_button_model_by);
        label.setTextSize(42);
        label.setMultipleLine(true);
        label.setTextAlignment(TextAlignment.CENTER);
        label.setTextColor(new Color(0xFFFFFFFF));
        label.setHeight(100);
        ll.addComponent(label);
        mLayout.addComponent(ll);
        return mLayout;
    }

    @Override
    public AExampleRenderer createRenderer() {
        return new FBXRenderer(getContext(), this);
    }

    private final class FBXRenderer extends AExampleRenderer {
        private Animation3D mAnim;

        FBXRenderer(Context context, AExampleFragment fragment) {
            super(context, fragment);
        }

        @Override
        protected void initScene() {
            mAnim = new RotateOnAxisAnimation(Vector3.Axis.Y, 360);
            mAnim.setDurationMilliseconds(16000);
            mAnim.setRepeatMode(Animation.RepeatMode.INFINITE);
            getCurrentScene().registerAnimation(mAnim);

            try {
                // -- Model by Sampo Rask
                // (http://www.blendswap.com/blends/characters/low-poly-rocks-character/)
                ResourceManager resManager = mContext.getResourceManager();
                RawFileEntry rawFileEntry = resManager.getRawFileEntry("resources/rawfile/"
                        + "lowpolyrocks_character_blendswap.fbx");
                LoaderFBX parser = new LoaderFBX(this, rawFileEntry);
                parser.parse(mContext);
                Object3D o = parser.getParsedObject();
                o.setY(-.5f);
                getCurrentScene().addChild(o);

                mAnim.setTransformable3D(o);
                mAnim.play();
            } catch (ParsingException e) {
                e.printStackTrace();
            } catch (Exception e) {
                e.printStackTrace();
            }

        }

    }

}
